//
//  HMEmoticonViewCell.swift
//  WeiBo
//
//  Created by Feng on 2017/6/14.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit

class HMEmoticonViewCell: UICollectionViewCell {
    
    //MARK: --懒加载控件
    //用于显示几行几列的Label
    private lazy var indexPathLabel: UILabel =
    {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 40)
        
        return label
    }()
    
    //删除按钮
    private lazy var deleteButton: UIButton =
    {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"compose_emotion_delete_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named:"compose_emotion_delete"), for: .normal)
        
        button.addTarget(self, action: #selector(deleteButtonClick), for: .touchUpInside)
        
        return button
    }()
    
    //存储图片按钮的数组
    var emoticonArray: [HMEmoticonButton] = [HMEmoticonButton]()
    
    //cell的模型
    var modelArray: [HMEmoticon]?
    {
        didSet {
            guard let currentArray = modelArray else {
                return
            }
            
            //先隐藏所有表情按钮
            for button in emoticonArray
            {
                button.isHidden = true
            }
            
            for (i,model) in currentArray.enumerated()
            {
                let button = emoticonArray[i]
                button.emoticon = model
//                //设置button为不隐藏
//                button.isHidden = false
//                
//                if model.type == "0"
//                {
//                    //  named: 指定的图片名表示在mainbundle里面加载这个图片，而我们的表情图片在emoticonbundle
//                    //  traitCollection: 图片的特性信息
//                    let image = UIImage(named: model.path!, in: HMEmoticonTools.sharedTools.emoticonBundle, compatibleWith: nil)
//                    
//                    button.setImage(image, for: .normal)
//                    button.setTitle(nil, for: .normal)
//                }
//                else
//                {
//                    let title = (model.code! as NSString).emoji()
//                    
//                    button.setImage(nil, for: .normal)
//                    button.setTitle(title, for: .normal)
//                }
            }
        }
    }
    
    //传入的IndexPath
    var indexPath: IndexPath?
    {
        didSet {
            
            guard let index = indexPath else {
                return
            }
            
            let section = index.section
            let item = index.item
            
            indexPathLabel.text = "第\(section)组第\(item)个"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
        addChildButton()
        contentView.addSubview(indexPathLabel)
        
        indexPathLabel.snp.makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
    }
    
    //MARK: --每个cell添加20个button
    private func addChildButton()
    {
        let buttonWidth = width / 7
        let buttonHeight = height / 3
        
        for i in 0..<20
        {
            //行索引
            let rowIndex = i / 7
            let columnIndex = i % 7
            
            let buttonX = buttonWidth * CGFloat(columnIndex)
            let buttonY = buttonHeight * CGFloat(rowIndex)
            
            let button: HMEmoticonButton = HMEmoticonButton(frame: CGRect(x: buttonX, y: buttonY, width: buttonWidth, height: buttonHeight)) 
            button.addTarget(self, action: #selector(emoticonButtonClick(btn:)), for: .touchUpInside)
            
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)

//            button.backgroundColor = RandomColor()
            contentView.addSubview(button)
            
            emoticonArray.append(button)
        }
        //添加删除按钮
        let deleteButtonX = self.width - buttonWidth
        let deleteButtonY = self.height - buttonHeight
        deleteButton.frame = CGRect(x: deleteButtonX, y: deleteButtonY, width: buttonWidth, height: buttonHeight)
        
        contentView.addSubview(deleteButton)
    }
    
    //MARK: --点击删除按钮的事件
    @objc private func deleteButtonClick()
    {
        //发送通知到VC ,删除textView中的表情
        NotificationCenter.default.post(name: NSNotification.Name(ClickDeleteButtonNotificationName), object: nil)
    }
    
    //MARK: --点击表情按钮的事件
    @objc private func emoticonButtonClick(btn: HMEmoticonButton)
    {
        //print(btn)
        let emoticonModel = btn.emoticon!
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(ClickEmotionButtonNotificationName), object: emoticonModel, userInfo: nil)
    }
}
